package com.os.crm.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.os.crm.mapper.RegionMapper;
import com.os.crm.pojo.Region;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import com.os.util.page.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RegionService {
    @Autowired
    private RegionMapper regionMapper;
    /**
     * 采用分页插件进行分页
     * @param pageRequest
     * @param map
     * @return
     */
    public PageResult selectByPage(PageRequest pageRequest, Map map) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Region> regionList = regionMapper.selectByParam(map);
        Tools.indexOrder(regionList,"index");
        PageInfo pageInfo = new PageInfo<Region>(regionList);
        return PageUtils.getPageResult(pageRequest, pageInfo);
    }
    /**
     * 按ID查询行政区划
     * @param id
     * @return
     */
    public Region selectById(int id){
        return regionMapper.selectById(id);
    }

    /**
     * 新增行政区划
     * @param region
     */
    public void insert(Region region){
        regionMapper.insertSelective(region);
    }
    /**
     * 修改行政区划
     * @param region
     */
    public void update(Region region){
        regionMapper.updateByPrimaryKeySelective(region);
    }

    /**
     * 按ID删除行政区划
     * @param id
     */
    public void deleteById(int id){
        regionMapper.deleteById(id);
    }
}
